import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import Welcome from '../components/Welcome.vue'
import users from '../components/user/users.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: users }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由首位设置
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokena = window.sessionStorage.getItem('token')
  if (!tokena) return next('/login')
  next()
}
)

export default router
